/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
///////////change log (new above)/////////
// added bad read counters, to stop sporadic misreadings to reset the bus
//////////////////////////////////////////
#include "frmpower.h"
#include <QtDebug>
#include <QSettings>
#include <QApplication>
#include <QMessageBox>
#include <iostream>
#include "qhkp.h"
#include <time.h>

#define DEBUG_ON    0
#define DEBUG if(DEBUG_ON)

FrmPower::BUS FrmPower::m_BorC = FrmPower::NONE;

FrmPower::FrmPower(BUS bus1,BUS bus2,QWidget *parent)
 : QWidget(parent),
 m_hpscEventThread(NULL),
 m_bus1(bus1),
 m_bus2(bus2),
 m_badReadLimit(12)
{
   QSettings settings("../../gss.conf",QSettings::IniFormat);
   m_hkpHost = settings.value("hosts/HKP_IP"/*,"127.0.0.1"*/).toString();
   m_hkpPort = settings.value("hosts/HKP_Port"/*,3010*/).toInt();
   m_hkpSocket = new QTcpSocket();
   
   if(m_hkpSocket && m_hkpPort>0)
    m_hkpSocket->connectToHost(m_hkpHost,m_hkpPort);
  
   m_busAtoleranceA = 0.5;
   m_busAtoleranceV = 0.5;
   m_busBtoleranceA = 0.5;
   m_busBtoleranceV = 0.5;
   m_busCtoleranceA = 0.5;
   m_busCtoleranceV = 0.5;
   m_busDtoleranceA = 0.5;
   m_busDtoleranceV = 0.5;
  
  //currently only checkout mode
  switch(bus1)
  {
    case A:m_src1v = 28.0;
           m_src1a = 0;//1.0;
           m_busAmaxA = m_src1a + m_busAtoleranceA;  //just test values, need to set correct values
           m_busAminA = m_src1a - m_busAtoleranceA;  //just test values, need to set correct values
           m_busAmaxV = m_src1v + m_busAtoleranceV;
           m_busAminV = m_src1v - m_busAtoleranceV;
           qDebug()<<"A maxV:"<<m_busAmaxV;
           qDebug()<<"A minV:"<<m_busAminV;
           qDebug()<<"A maxA:"<<m_busAmaxA;
           qDebug()<<"A minA:"<<m_busAminA;           
      break;
    case B:
           m_src2v = 6.0;
           m_src2a = 0;//2.5;
           m_src3v = 6.0;
           m_src3a = 0;//2.5;
           m_busBmaxA = m_src2a+m_src3a + m_busBtoleranceA;  //just test values, need to set correct values
           m_busBminA = m_src2a+m_src3a - m_busBtoleranceA;  //just test values, need to set correct values
           m_busBmaxV = m_src2v + m_src3v + m_busBtoleranceV;
           m_busBminV = m_src2v + m_src3v - m_busBtoleranceV;
           qDebug()<<"B maxV:"<<m_busBmaxV;
           qDebug()<<"B minV:"<<m_busBminV;
           qDebug()<<"B maxA:"<<m_busBmaxA;
           qDebug()<<"B minA:"<<m_busBminA;
           
      break;
    case C:
          m_src2v = 14.0;
          m_src2a = 0;//3.0;
          m_src3v = 14.0;
          m_src3a = 0;//3.0;
          m_busCmaxA = m_src2a+m_src3a+m_busCtoleranceA;  //just test values, need to set correct values
          m_busCminA = m_src2a+m_src3a-m_busCtoleranceA;  //just test values, need to set correct values
          m_busCmaxV = m_src2v + m_src3v + m_busCtoleranceV;
          m_busCminV = m_src2v + m_src3v - m_busCtoleranceV;
          qDebug()<<"C maxV:"<<m_busCmaxV;
          qDebug()<<"C minV:"<<m_busCminV;
          qDebug()<<"C maxA:"<<m_busCmaxA;
          qDebug()<<"C minA:"<<m_busCminA;
          
      break;
    case D:m_src4v = 0.0; //subject to batt charge profile
           m_src4a = 0;//2.0;
           m_busDmaxA = m_src4a + m_busDtoleranceA;  //just test values, need to set correct values
           m_busDminA = m_src4a - m_busDtoleranceA;  //just test values, need to set correct values
           m_busDmaxV = m_src4v + m_busDtoleranceV;
           m_busDminV = m_src4v - m_busDtoleranceV;
           qDebug()<<"D maxV:"<<m_busDmaxV;
           qDebug()<<"D minV:"<<m_busDminV;
           qDebug()<<"D maxA:"<<m_busDmaxA;
           qDebug()<<"D minA:"<<m_busDminA;
      break;
    case NONE: //keep the compiler happy
        break;  
  }
  
  switch(bus2)
  {
      case A:m_src1v = 28.0;
      m_src1a = 0;//1.0;
      m_busAmaxA = m_src1a + m_busAtoleranceA;  //just test values, need to set correct values
      m_busAminA = m_src1a - m_busAtoleranceA;  //just test values, need to set correct values
      m_busAmaxV = m_src1v + m_busAtoleranceV;
      m_busAminV = m_src1v - m_busAtoleranceV;
      qDebug()<<"A maxV:"<<m_busAmaxV;
      qDebug()<<"A minV:"<<m_busAminV;
      qDebug()<<"A maxA:"<<m_busAmaxA;
      qDebug()<<"A minA:"<<m_busAminA;           
      break;
      case B:
          m_src2v = 6.0;
          m_src2a = 0;//2.5;
          m_src3v = 6.0;
          m_src3a = 0;//2.5;
          m_busBmaxA = m_src2a+m_src3a + m_busBtoleranceA;  //just test values, need to set correct values
          m_busBminA = m_src2a+m_src3a - m_busBtoleranceA;  //just test values, need to set correct values
          m_busBmaxV = m_src2v + m_src3v + m_busBtoleranceV;
          m_busBminV = m_src2v + m_src3v - m_busBtoleranceV;
          qDebug()<<"B maxV:"<<m_busBmaxV;
          qDebug()<<"B minV:"<<m_busBminV;
          qDebug()<<"B maxA:"<<m_busBmaxA;
          qDebug()<<"B minA:"<<m_busBminA;
           
          break;
      case C:
          m_src2v = 14.0;
          m_src2a = 0;//3.0;
          m_src3v = 14.0;
          m_src3a = 0;//3.0;
          m_busCmaxA = m_src2a+m_src3a+m_busCtoleranceA;  //just test values, need to set correct values
          m_busCminA = m_src2a+m_src3a-m_busCtoleranceA;  //just test values, need to set correct values
          m_busCmaxV = m_src2v + m_src3v + m_busCtoleranceV;
          m_busCminV = m_src2v + m_src3v - m_busCtoleranceV;
          qDebug()<<"C maxV:"<<m_busCmaxV;
          qDebug()<<"C minV:"<<m_busCminV;
          qDebug()<<"C maxA:"<<m_busCmaxA;
          qDebug()<<"C minA:"<<m_busCminA;
          
          break;
          case D:m_src4v = 0.0; //subject to batt charge profile
          m_src4a = 0;//2.0;
          m_busDmaxA = m_src4a + m_busDtoleranceA;  //just test values, need to set correct values
          m_busDminA = m_src4a - m_busDtoleranceA;  //just test values, need to set correct values
          m_busDmaxV = m_src4v + m_busDtoleranceV;
          m_busDminV = m_src4v - m_busDtoleranceV;
          qDebug()<<"D maxV:"<<m_busDmaxV;
          qDebug()<<"D minV:"<<m_busDminV;
          qDebug()<<"D maxA:"<<m_busDmaxA;
          qDebug()<<"D minA:"<<m_busDminA;
          break;
          case NONE: //keep the compiler happy
              break;  
    } 

    ui.setupUi(this);
    

    ui.btnBus1->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/hpsc/btn_v_disabled.png"));
    ui.btnBus1->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/hpsc/btn_v_on_pressed.png"));
    ui.btnBus1->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/hpsc/btn_v_off_pressed.png"));
    ui.btnBus1->replaceStatePixmap(PStyledButton::on,QPixmap(":images/hpsc/btn_v_on.png"));
    ui.btnBus1->replaceStatePixmap(PStyledButton::off,QPixmap(":images/hpsc/btn_v_off.png"));
    ui.btnSetBus1->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/hpsc/btn_set_HPSC_pressed.png"));
    ui.btnBus2->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/hpsc/btn_v_disabled.png"));
    ui.btnBus2->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/hpsc/btn_v_off_pressed.png"));
    ui.btnBus2->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/hpsc/btn_v_on_pressed.png"));
    ui.btnBus2->replaceStatePixmap(PStyledButton::on,QPixmap(":images/hpsc/btn_v_on.png"));
    ui.btnBus2->replaceStatePixmap(PStyledButton::off,QPixmap(":images/hpsc/btn_v_off.png"));
    ui.btnSetBus2->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/hpsc/btn_set_HPSC_pressed.png"));
    
	switch(bus1)
	{
		case A:	
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_a.png"));
                ui.lblPower1->setText("Power Bus A");
                break;
		case B:
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_b.png"));
                ui.lblPower1->setText("Power Bus B");
                break;
		case C:
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_c.png")); 
                ui.lblPower1->setText("Power Bus C");
                break;
		case D:
                ui.lblBus1Status->setPixmap(QPixmap(":images:/hpsc/lbl_power_off_d.png"));
                ui.lblPower1->setText("Power Bus D");
                break;
        case NONE: //keep the compiler happy
            break;
	}
	switch(bus2)
	{
		case A:
                ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_a.png"));
                ui.lblPower2->setText("Power Bus A");
                break;
		case B: 
                ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_b.png"));
                ui.lblPower2->setText("Power Bus B");
                break;
		case C: 
                ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_c.png"));
                ui.lblPower2->setText("Power Bus C");
                break;
		case D: 
                ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_d.png"));
                ui.lblPower2->setText("Power Bus D");
                break;
        case NONE: //keep the compiler happy
            break;
	}
     
	connect(ui.btnBus1,SIGNAL(clicked(bool)),this,SLOT(bus1(bool)));
	connect(ui.btnBus2,SIGNAL(clicked(bool)),this,SLOT(bus2(bool)));
    connect(ui.btnSetBus1,SIGNAL(clicked()),this,SLOT(setBus1()));
    connect(ui.btnSetBus2,SIGNAL(clicked()),this,SLOT(setBus2()));
}


FrmPower::~FrmPower()
{
}

/*!
    \fn FrmPower::btnVolt1On()
 */
PStyledButton * FrmPower::btnBus1()
{
    return ui.btnBus1;
}

/*!
    \fn FrmPower::btnVolt2()
 */
PStyledButton * FrmPower::btnBus2()
{
	return ui.btnBus2;
}

/*!
    \fn FrmPower::setAlabel(int busNr,float val)
 */
void FrmPower::setAlabel(int busNr,float val)
{
   QString sVal = QString::number(val);
	switch(busNr)
   {
	   case 1: ui.lblmA1->setText(sVal);
		   break;
	   case 2: ui.lblmA2->setText(sVal);
			break;
   }
}

/*!
    \fn FrmPower::setVlabel(int busNr,float val)
 */
void FrmPower::setVlabel(int busNr,float val)
{
	QString sVal = QString::number(val);
	switch(busNr)
	{
		case 1: ui.lblV1->setText(sVal);
		break;
		case 2: ui.lblV2->setText(sVal);
		break;
	}
}

/*!
    \fn FrmPower::bus2()
 */
void FrmPower::bus2(bool checked)
{
    HkpCommand command;
    HPSC_Command hpscCommand;
	switch(m_bus2)
	{
        case A:	
            command.type = WRITE;
            command.port = QHkp::A;
            command.pin = CHAN_1;
            if(checked && checkBusV(A)==POWER_OK)
            {
                hpscCommand.cmd = BUS1;
                m_hpscEventThread->eventHandler()->socket()->flush();
                ui.lblBus2Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_a.png"));
                btnBus2()->setState(PStyledButton::on);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_ON;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            else {
                //if got into else because previous if() is false, then button is checked
                if(checked) btnBus2()->setChecked(false);
                
                btnBus2()->setState(PStyledButton::off);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_OFF;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
			break;
		case B:
                if(FrmPower::getBorC() != FrmPower::C)
                {
                    command.type = WRITE;
                    command.port = QHkp::A;
                    command.pin = CHAN_2;
                    if(checked && checkBusV(B)==POWER_OK)
                    {
                        hpscCommand.cmd = BUS2;
                        m_hpscEventThread->eventHandler()->socket()->flush();
                        ui.lblBus2Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_b.png"));
                        btnBus2()->setState(PStyledButton::on);
                        QDataStream hkpIn(m_hkpSocket);
                        command.mode = PIN_ON;
                        command.pin = BUS_B_SELECT|CHAN_2 | CHAN_3;
                        hkpIn<<command;
                        command.mode = PIN_OFF;
                        command.pin = BUS_C_SELECT;
                        hkpIn<<command;
                        m_hkpSocket->flush();
                    }
                    else {
                        
                        //if got into else because previous if() is false, then button is checked
                        if(checked) btnBus2()->setChecked(false);
                        
                        btnBus2()->setState(PStyledButton::off);
                        QDataStream hkpIn(m_hkpSocket);
                        command.mode = PIN_OFF;
                        command.pin = BUS_B_SELECT|CHAN_2|CHAN_3;
                        hkpIn<<command;
                        m_hkpSocket->flush();
                    }
                }
                else QMessageBox(QMessageBox::Critical,"Channel Conflict","Can't do that while channel C is on");
			break;
		case C:
            if(FrmPower::getBorC() != FrmPower::B)
            {
                command.type = WRITE;
                command.port = QHkp::A;
                command.pin = CHAN_2;
                if(checked && checkBusV(C)==POWER_OK)
                {
                    hpscCommand.cmd = BUS3;
                    m_hpscEventThread->eventHandler()->socket()->flush();
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_c.png"));
                    btnBus2()->setState(PStyledButton::on);
                    QDataStream hkpIn(m_hkpSocket);
                    command.mode = PIN_ON;
                    command.pin = BUS_C_SELECT|CHAN_2  | CHAN_3;
                    hkpIn<<command;
                    command.mode = PIN_OFF;
                    command.pin = BUS_B_SELECT;
                    hkpIn<<command;
                    m_hkpSocket->flush();
                }
                else {
                    
                    //if got into else because previous if() is false, then button is checked
                    if(checked) btnBus2()->setChecked(false);
                    btnBus2()->setState(PStyledButton::off);
                    QDataStream hkpIn(m_hkpSocket);
                    command.mode = PIN_OFF;
                    command.pin = BUS_C_SELECT|CHAN_2 | CHAN_3;
                    hkpIn<<command;
                    m_hkpSocket->flush();
                }
            }
			break;
		case D:
            command.type = WRITE;
            command.port = QHkp::A;
            command.pin = CHAN_4;
            if(checked && checkBusV(D)==POWER_OK)
            {
                hpscCommand.cmd = BUS4;
                m_hpscEventThread->eventHandler()->socket()->flush();
                ui.lblBus2Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_d.png"));
                btnBus2()->setState(PStyledButton::on);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_ON;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            else {
                
                //if got into else because previous if() is false, then button is checked
                if(checked) btnBus2()->setChecked(false);
                btnBus2()->setState(PStyledButton::off);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_OFF;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            break;
        case NONE: //keep the compiler happy
            break;
	}

}


/*!
    \fn FrmPower::bus1()
    Sets BUS1 on through Hacker Port
 */
void FrmPower::bus1(bool checked)
{
    HkpCommand command;
    HPSC_Command hpscCommand;
    
	switch(m_bus1)
    {
      case A:
            command.type = WRITE;
            command.port = QHkp::A;
            command.pin = CHAN_1;
            if(checked && checkBusV(A)==POWER_OK)
            {
                btnBus1()->setState(PStyledButton::on);
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_a.png"));
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_ON;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            else {
                    //if got into else because previous if() is false, then button is checked
                    if(checked) btnBus1()->setChecked(false);
                    btnBus1()->setState(PStyledButton::off);
                    QDataStream hkpIn(m_hkpSocket);
                    command.mode = PIN_OFF;
                    hkpIn<<command;
                    m_hkpSocket->flush();
            }
        break;
	 case B:
            command.type = WRITE;
            command.port = QHkp::A;
            DEBUG qDebug()<<"state:"<<(int)btnBus1()->state();
            if(checked && checkBusV(B)==POWER_OK)
            {
                btnBus1()->setState(PStyledButton::on);
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_b.png"));
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_ON;
                command.pin = BUS_B_SELECT|CHAN_2 | CHAN_3;
                hkpIn<<command;
                m_hkpSocket->flush();
                command.mode = PIN_OFF;
                command.pin = BUS_C_SELECT;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            else {
                    //if got into else because previous if() is false, then button is checked
                    if(checked) btnBus1()->setChecked(false);    
                    btnBus1()->setState(PStyledButton::off);
                    QDataStream hkpIn(m_hkpSocket);
                    command.mode = PIN_OFF;
                    command.pin = BUS_B_SELECT|CHAN_2 | CHAN_3;
                    hkpIn<<command;
                    m_hkpSocket->flush();
            }
            break;
      case C:
            command.type = WRITE;
            command.port = QHkp::A;
            command.pin = CHAN_2;
            DEBUG qDebug()<<"state:"<<(int)btnBus1()->state();
            if(checked && checkBusV(C)==POWER_OK)
            {
                btnBus1()->setState(PStyledButton::on);
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_c.png"));
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_ON;
                command.pin = BUS_C_SELECT|CHAN_2|CHAN_3;
                hkpIn<<command;
                m_hkpSocket->flush();
                command.mode = PIN_OFF;
                command.pin = BUS_B_SELECT;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            else {
                //if got into else because previous if() is false, then button is checked
                if(checked) btnBus1()->setChecked(false);
                btnBus1()->setState(PStyledButton::off);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_OFF;
                command.pin = BUS_C_SELECT|CHAN_2|CHAN_3;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            break;
      case D:
            command.type = WRITE;
            command.port = QHkp::A;
            command.pin = CHAN_4;
            if(checked && checkBusV(D)==POWER_OK)
            {
                hpscCommand.cmd = BUS1;
                m_hpscEventThread->eventHandler()->socket()->flush();
                ui.lblBus1Status->setPixmap(QPixmap(":images/hpcs/lbl_power_ok_d.png"));  
                btnBus1()->setState(PStyledButton::on);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_ON;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            else {
                //if got into else because previous if() is false, then button is checked and needs to be unchecked
                if(checked) btnBus1()->setChecked(false);
                btnBus1()->setState(PStyledButton::off);
                QDataStream hkpIn(m_hkpSocket);
                command.mode = PIN_OFF;
                hkpIn<<command;
                m_hkpSocket->flush();
            }
            break;
        case NONE: //keep the compiler happy
            break;
    }
}

/*!
    \fn FrmPower::connectHandler()
 */
void FrmPower::connectHandler()
{
	DEBUG qDebug()<<"FrmPower::connectHandler()";
    qRegisterMetaType<HPSC_Command>(); //for inter thread signals, custom types need to be registered
    connect(m_hpscEventThread->eventHandler(),SIGNAL(sig_hpscReadoutA(int,float)),this,SLOT(hpscReadoutA(int,float)));
    connect(m_hpscEventThread->eventHandler(),SIGNAL(sig_hpscReadoutV(int,float)),this,SLOT(hpscReadoutV(int,float)));
    connect(this,SIGNAL(sig_hpscCommand(HPSC_Command,int)),m_hpscEventThread->eventHandler(),SLOT(sendHPSCCommand(HPSC_Command,int)));
}

/*!
    \fn FrmPower::setHPSCEventThread(HPSCEventThread *hpscThread)
 */
void FrmPower::setHPSCEventThread(HPSCEventThread *hpscThread)
{
  DEBUG qDebug()<<"FrmPower::setHPSCEventThread";
  m_hpscEventThread = hpscThread;
  if(m_hpscEventThread)
  {
      DEBUG qDebug()<<"FrmPower::setHPSCEventThread: before starting thread";
    if(!m_hpscEventThread->isRunning())
    {
        DEBUG qDebug()<<"FrmPower::setHPSCEventThread: starting thread";  
      m_hpscEventThread->start();
        connect(m_hpscEventThread,SIGNAL(started()),this,SLOT(connectHandler()));
        connect(m_hpscEventThread,SIGNAL(started()),this,SLOT(connectHandler()));
        connect(m_hpscEventThread,SIGNAL(sig_hpscDisconnected()),this,SLOT(hpscDisconnected()));
    }
    else connectHandler();
  }
}


/*!
    \fn FrmPower::hpscDisconnected()
 */
void FrmPower::hpscDisconnected()
{
    DEBUG qDebug()<<"CONNECTION TO HPSC SOCKET LOST!";
}


/*!
    \fn FrmPower::setBus2()
 */
void FrmPower::setBus2()
{
    HPSC_Command hpscCommand;
    
    switch(m_bus2)
    {
        case A:
            hpscCommand.cmd = BUS1;
            emit sig_hpscCommand(hpscCommand,HPSCEvent); //inter thread signal
            break;
        case B:
            if(FrmPower::getBorC() != FrmPower::C)
            {
                hpscCommand.cmd = BUS2;
                emit sig_hpscCommand(hpscCommand,HPSCEvent); //inter thread signal
            }
            else std::cout<<"WARNING: can't set BUS B while BUS C is on!"<<std::endl;
            break;
        case C:
            if(FrmPower::getBorC() != FrmPower::B)
            {
                hpscCommand.cmd = BUS3;
                emit sig_hpscCommand(hpscCommand,HPSCEvent); //inter thread signal
            }
            else std::cout<<"WARNING: can't set BUS C while BUS B is on!"<<std::endl;
            break;
        case D:
            hpscCommand.cmd = BUS4;
            emit sig_hpscCommand(hpscCommand,HPSCEvent); //inter thread signal
            break;
        case NONE: //keep the compiler happy
            break;
    }
}


/*!
    \fn FrmPower::setBus1()
 */
void FrmPower::setBus1()
{
    HPSC_Command hpscCommand;
    switch(m_bus1)
    {
        case A:
            hpscCommand.cmd = BUS1;
            emit sig_hpscCommand(hpscCommand,(int)HPSCEvent);//inter thread signal
            break;
        case B:
            if(FrmPower::getBorC() != FrmPower::C)
            {
                hpscCommand.cmd = BUS2;
                emit sig_hpscCommand(hpscCommand,(int)HPSCEvent);//inter thread signal
            }
            else std::cout<<"WARNING: can't set BUS B while BUS C is on!"<<std::endl;
            break;
        case C:
            if(FrmPower::getBorC() != FrmPower::B)
            {
                hpscCommand.cmd = BUS3;
                emit sig_hpscCommand(hpscCommand,(int)HPSCEvent);//inter thread signal
            }
            else std::cout<<"WARNING: can't set BUS C while BUS B is on!"<<std::endl;
            break;
        case D:
            hpscCommand.cmd = BUS4;
            emit sig_hpscCommand(hpscCommand,(int)HPSCEvent);//inter thread signal
            break;
        case NONE: //keep the compiler happy
            break;
    }
}


/*!
    \fn FrmPower::hpscReadoutV(int src,float volt)
 */
void FrmPower::hpscReadoutV(int src,float volt)
{
    switch(m_bus1)
    {
        case A:
            if(src==1)
            {
                ui.lblV1->setText(QString("%1").arg(volt,0,'f',1));
                m_src1vRead = volt;
                if(btnBus1()->state() ==  PStyledButton::on)
                {
                    setStatusBus1(A, checkBus(A));
                }
                else setStatusBus1(A,POWER_OFF);
            }
            break;
        case B:
            if(src==2)
            {
                ui.lblV1->setText(QString("%1").arg(volt+m_src3vRead,0,'f',1));
                m_src2vRead = volt;
                if(btnBus1()->state() ==  PStyledButton::on)
                {
                    
                    setStatusBus1(B,checkBus(B));                
                }
                else setStatusBus1(B,POWER_OFF);
            }
            if(src==3)
            {
                
                ui.lblV1->setText(QString("%1").arg(volt+m_src2vRead,0,'f',1));
                m_src3vRead = volt;
                if(btnBus1()->state() ==  PStyledButton::on)
                {
                    setStatusBus1(B,checkBus(B));
                }
                else setStatusBus1(B,POWER_OFF);            
            }
            break;
        case C:
            if(src==2)
            {
                ui.lblV1->setText(QString("%1").arg(volt+m_src3vRead,0,'f',1));
                m_src2vRead = volt;
                if(btnBus1()->state() ==  PStyledButton::on)
                {
                    setStatusBus1(C,checkBus(C));
                }
                else setStatusBus1(C,POWER_OFF);
            }
            if(src==3)
            {
                ui.lblV1->setText(QString("%1").arg(volt+m_src2vRead,0,'f',1));
                m_src3vRead = volt;
                if(btnBus1()->state() ==  PStyledButton::on)
                {
                    setStatusBus1(C,checkBus(C));
                }
                else setStatusBus1(C,POWER_OFF);
            }
            break;
        case D:
            if(src==4)
            {
                ui.lblV1->setText(QString("%1").arg(volt,0,'f',1));
                m_src4vRead = volt;
                if(btnBus1()->state() ==  PStyledButton::on)
                {
                    setStatusBus1(D,checkBus(D));
                }
                else setStatusBus1(D,POWER_OFF);
            }
            break;
        case NONE: //keep the compiler happy
            break;
    }
    
    
    switch(m_bus2)
    {
        case A:
            if(src==1)
            {
                ui.lblV2->setText(QString("%1").arg(volt,0,'f',1));
                m_src1vRead = volt;
                if(btnBus2()->state() ==  PStyledButton::on)
                {
                    setStatusBus2(A,checkBus(A));
                }
                else setStatusBus2(A,POWER_OFF);
            }
            break;
        case B:
            if(src==2)
            {
                DEBUG qDebug()<<"src:"<<src<<" volt:"<<volt<<" m_src3vRead:"<<m_src3vRead;
                ui.lblV2->setText(QString("%1").arg(volt+m_src3vRead,0,'f',1));
                m_src2vRead = volt;
                if(btnBus2()->state() ==  PStyledButton::on)
                {
                    setStatusBus2(B,checkBus(B));
                }
                else setStatusBus2(B,POWER_OFF);
                
            }
            if(src==3)
            {
                DEBUG qDebug()<<"src:"<<src<<" volt:"<<volt<<" m_src2vRead:"<<m_src2vRead;
                ui.lblV2->setText(QString("%1").arg(volt+m_src2vRead,0,'f',1));
                m_src3vRead = volt;
                if(btnBus2()->state() ==  PStyledButton::on)
                {
                    setStatusBus2(B,checkBus(B));
                }
                else setStatusBus2(B,POWER_OFF);
                
            }
            break;
        case C:
            if(src==2)
            {
                ui.lblV2->setText(QString("%1").arg(volt+m_src3vRead,0,'f',1));
                m_src2vRead = volt;
                if(btnBus2()->state() ==  PStyledButton::on)
                {
                    setStatusBus2(C,checkBus(C));
                }
                else setStatusBus2(C,POWER_OFF);
                
            }
            if(src==3)
            {
                ui.lblV2->setText(QString("%1").arg(volt+m_src2vRead,0,'f',1));
                m_src3vRead = volt;
                if(btnBus2()->state() ==  PStyledButton::on)
                {
                    setStatusBus2(C,checkBus(C));
                }
                else setStatusBus2(C,POWER_OFF);
                
            }
            break;
        case D:
            if(src==4)
            {
                ui.lblV2->setText(QString("%1").arg(volt,0,'f',1));
                m_src4vRead = volt;
                if(btnBus2()->state() ==  PStyledButton::on)
                {
                    setStatusBus2(D,checkBus(D));
                }
                else setStatusBus2(D,POWER_OFF);
                
            }
            break;
        case NONE: //keep the compiler happy
            break;
    } 
    
}


/*!
    \fn FrmPower::hpscReadoutA(int src,float amper)
 */
void FrmPower::hpscReadoutA(int src,float amper)
{
    switch(m_bus1)
    {
        case A:
            if(src==1)
            {
                ui.lblmA1->setText(QString("%1").arg(amper,0,'f',1));
                m_src1aRead = amper;
                if( btnBus1()->state() == PStyledButton::on)
                {
                    setStatusBus1(A,checkBus(A));
                }
                else setStatusBus1(A,POWER_OFF); 
                    
            }
            break;
        case B:
            if(src==2)
            {
                ui.lblmA1->setText(QString("%1").arg(amper+m_src3aRead,0,'f',1));
                m_src2aRead = amper;
                if( btnBus1()->state() == PStyledButton::on)
                {
                    setStatusBus1(B,checkBus(B));
                }
                else setStatusBus1(B,POWER_OFF); 
                
            }
            if(src==3)
            {
                ui.lblmA1->setText(QString("%1").arg(amper+m_src2aRead,0,'f',1));
                m_src3aRead = amper;
                if( btnBus1()->state() == PStyledButton::on)
                {
                    setStatusBus1(B,checkBus(B));
                }
                else setStatusBus1(B,POWER_OFF); 
                
            }
            break;
        case C:
            if(src==2)
            {
                ui.lblmA1->setText(QString("%1").arg(amper+m_src3aRead,0,'f',1));
                m_src2aRead = amper;
                if( btnBus1()->state() == PStyledButton::on)
                {
                    setStatusBus1(C,checkBus(C));
                }
                else setStatusBus1(C,POWER_OFF); 
                
            }
            if(src==3)
            {
                ui.lblmA1->setText(QString("%1").arg(amper+m_src2aRead,0,'f',1));
                m_src3aRead = amper;
                if( btnBus1()->state() == PStyledButton::on)
                {
                    setStatusBus1(C,checkBus(C));
                }
                else setStatusBus1(C,POWER_OFF); 
                
            }
            break;
        case D:
            if(src==4)
            {
                ui.lblmA1->setText(QString("%1").arg(amper,0,'f',1));
                m_src4aRead = amper;
                if( btnBus1()->state() == PStyledButton::on)
                {
                    setStatusBus1(D,checkBus(D));
                }
                else setStatusBus1(D,POWER_OFF); 
                
            }
            break;
        case NONE: //keep the compiler happy
            break;
    }
    
    switch(m_bus2)
    {
        case A:
            if(src==1)
            {
                ui.lblmA2->setText(QString("%1").arg(amper,0,'f',1));
                m_src1aRead = amper;
                if( btnBus2()->state() == PStyledButton::on)
                {
                    setStatusBus2(A,checkBus(A));
                }
                else setStatusBus2(A,POWER_OFF); 
               
            }
            break;
        case B:
            if(src==2)
            {
                ui.lblmA2->setText(QString("%1").arg(amper+m_src3aRead,0,'f',1));
                m_src2aRead = amper;
                if( btnBus2()->state() == PStyledButton::on)
                {
                    setStatusBus2(B,checkBus(B));
                }
                else setStatusBus2(B,POWER_OFF); 
                
            }
            if(src==3)
            {
                ui.lblmA2->setText(QString("%1").arg(amper+m_src2aRead,0,'f',1));
                m_src3aRead = amper;
                if( btnBus2()->state() == PStyledButton::on)
                {
                    setStatusBus2(B,checkBus(B));
                }
                else setStatusBus2(B,POWER_OFF); 
                
            }
            break;
        case C:
            if(src==2)
            {
                ui.lblmA2->setText(QString("%1").arg(amper+m_src3aRead,0,'f',1));
                m_src2aRead = amper;
                if( btnBus2()->state() == PStyledButton::on)
                {
                    setStatusBus2(C,checkBus(C));
                }
                else setStatusBus2(C,POWER_OFF); 
                
            }
            if(src==3)
            {
                ui.lblmA2->setText(QString("%1").arg(amper+m_src2aRead,0,'f',1));
                m_src3aRead = amper;
                if( btnBus2()->state() == PStyledButton::on)
                {
                    setStatusBus2(C,checkBus(C));
                }
                else setStatusBus2(C,POWER_OFF); 
                
            }
            break;
        case D:
            if(src==4)
            {
                ui.lblmA2->setText(QString("%1").arg(amper,0,'f',1));
                m_src4aRead = amper;
                if( btnBus2()->state() == PStyledButton::on)
                {
                    setStatusBus2(D,checkBus(D));
                }
                else setStatusBus2(D,POWER_OFF); 
                
            }
            break;
        case NONE: //keep the compiler happy
            break;
    }
    
    QCoreApplication::processEvents();
}



/*!
    \fn FrmPower::getHkpStatus(QHkp::port port, quint8 data)
    Responds to the signal sig_hkp() from the main GUI window.
    Uses the signal to check the B-C select line, and update the buttons accourdingly 
 **/
void FrmPower::getHkpStatus(HkpCommand hkpPacket)
{
    if(FrmPower::getBorC() == FrmPower::B)
    {
        if(m_bus1 == FrmPower::C)
            btnBus1()->setEnabled(false);
        
        if(m_bus2 == FrmPower::C)
            btnBus2()->setEnabled(false);
    }
    
    if(FrmPower::getBorC() == FrmPower::C)
    {
        if(m_bus1 == FrmPower::B)
            btnBus1()->setEnabled(false);
        
        if(m_bus2 == FrmPower::B)
            btnBus2()->setEnabled(false);
    }
    
    if(FrmPower::getBorC() == FrmPower::NONE)
    {
        if(!btnBus1()->isEnabled()) btnBus1()->setEnabled(true);
        if(!btnBus2()->isEnabled()) btnBus2()->setEnabled(true);
    }
}


/*!
    \fn FrmPower::getBorC() const
 */
FrmPower::BUS FrmPower::getBorC() 
{
    return FrmPower::m_BorC;
}



/*!
    \fn FrmPower::setBorC(FrmPower::BUS bc)
 */
void FrmPower::setBorC(FrmPower::BUS bc)
{
    FrmPower::m_BorC = bc;
}


/*!
    \fn FrmPower::checkBusV(BUS bus)
 */
FrmPower::POWER_STAT FrmPower::checkBusV(BUS bus)
{
    int reset=0;
    switch(bus)
    {
        case A:
            if(m_src1vRead > m_busAmaxV && (m_badReadCntA_V >= m_badReadLimit)) return OVERVOLT;
            else if(m_src1vRead > m_busAmaxV) m_badReadCntA_V++;
                else reset++;
            if(m_src1vRead < m_busAminV && (m_badReadCntA_V >= m_badReadLimit)) return UNDERVOLT;
            else if(m_src1vRead < m_busAmaxV) m_badReadCntA_V++;
                else if(reset) m_badReadCntA_V = 0;
            
            return POWER_OK;
            break;
        case B:
            if(m_src2vRead + m_src3vRead > m_busBmaxV && (m_badReadCntB_V >= m_badReadLimit)) return OVERVOLT;
            else if(m_src2vRead + m_src3vRead > m_busBmaxV) m_badReadCntB_V++;
                else reset++;
            if(m_src3vRead + m_src3vRead < m_busBminV && (m_badReadCntB_V >= m_badReadLimit)) return UNDERVOLT;
            else if(m_src3vRead + m_src3vRead < m_busBminV) m_badReadCntB_V++;
                else if(reset) m_badReadCntB_V = 0;
            
            return POWER_OK;
            break;
        case C:
            if(m_src2vRead + m_src3vRead > m_busCmaxV && (m_badReadCntC_V >= m_badReadLimit)) return OVERVOLT;
            else if(m_src2vRead + m_src3vRead > m_busCmaxV) m_badReadCntC_V++;
                else reset++;
            if(m_src2vRead + m_src3vRead < m_busCminV && (m_badReadCntC_V >= m_badReadLimit)) return UNDERVOLT;
            else if(m_src2vRead + m_src3vRead < m_busCminV) m_badReadCntC_V++;
                 else if(reset) m_badReadCntC_V=0;
            return POWER_OK;
  
            break;
        case D:
            if(m_src4vRead > m_busDmaxV && (m_badReadCntD_V >= m_badReadLimit) ) return OVERVOLT;
            else  if(m_src4vRead > m_busDmaxV)m_badReadCntD_V++;
                  else reset++;
            if(m_src4vRead < m_busDminV && (m_badReadCntD_V >= m_badReadLimit)) return UNDERVOLT;
            else if(m_src4vRead < m_busDminV) m_badReadCntD_V++;
                 else if(reset) m_badReadCntD_V = 0;
            
            return POWER_OK;
            
            break;
        case NONE: //keep the compiler happy
            break;
    }
            
}
    
    


/*!
    \fn FrmPower::checkBusA(BUS bus)
 */
FrmPower::POWER_STAT FrmPower::checkBusA(BUS bus)
{
    switch(bus)
    {
        case A:
            if(m_src1aRead > m_busAmaxA && m_badReadCntA_A >= m_badReadLimit ) return AMPLIMIT;
            else if(m_src1aRead > m_busAmaxA) m_badReadCntA_A++;
                 else m_badReadCntA_A = 0;
            
            return POWER_OK;
            break;
        case B:
            if(m_src2aRead + m_src3aRead > m_busBmaxA && (m_badReadCntB_A >= m_badReadLimit )) return AMPLIMIT;
            else if(m_src2aRead + m_src3aRead > m_busBmaxA) m_badReadCntB_A++;
                 else m_badReadCntB_A = 0;
            return POWER_OK;
            break;
        case C:
            if(m_src2aRead + m_src3aRead > m_busCmaxA && (m_badReadCntC_A >= m_badReadLimit )) return AMPLIMIT;
            else if(m_src2aRead + m_src3aRead > m_busCmaxA) m_badReadCntC_A++;
                 else m_badReadCntC_A = 0;
            return POWER_OK;
            break;
        case D:
            if(m_src4aRead > m_busDmaxA && (m_badReadCntD_A >= m_badReadLimit )) return AMPLIMIT;
            else if(m_src4aRead > m_busDmaxA) m_badReadCntD_A++;
                 else m_badReadCntD_A = 0;
            return POWER_OK;
            break;
        case NONE: //keep the compiler happy
            break;
    }
}



/*!
    \fn FrmPower::setAtolerance(BUS bus, float tolerance)
 */
void FrmPower::setAtolerance(BUS bus, float tolerance)
{
    switch(bus)
    {
        case A:
            m_busAtoleranceV = tolerance;
            break;
        case B:
            m_busBtoleranceV = tolerance;
            break;
        case C:
            m_busCtoleranceV = tolerance;
            break;
        case D:
            m_busDtoleranceV = tolerance;
            break;
        default: //keep the compiler happy
            break;
    }
}


/*!
    \fn FrmPower::setVtolerance(BUS bus, float tolerance)
 */
void FrmPower::setVtolerance(BUS bus, float tolerance)
{
    switch(bus)
    {
        case A:
            m_busAtoleranceA = tolerance;
            break;
        case B:
            m_busBtoleranceA = tolerance;
            break;
        case C:
            m_busCtoleranceA = tolerance;
            break;
        case D:
            m_busDtoleranceA = tolerance;
            break;
        default: //keep the compiler happy
            break;
    }
}


/*!
    \fn FrmPower::setStatusBus2(BUS bus,POWER_STAT status)
 */
void FrmPower::setStatusBus2(BUS bus,POWER_STAT status)
{
    
    switch(status)
    {
        case POWER_OFF:
            switch(bus)
            {
                case A:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_a.png"));
                    break;
                case B:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_b.png"));
                    break;
                case C:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_c.png"));
                    break;
                case D:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case POWER_OK:
            switch(bus)
            {
                case A:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_a.png"));
                    break;
                case B:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_b.png"));
                    break;
                case C:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_c.png"));
                    break;
                case D:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case OVERVOLT:
            switch(bus)
            {
                case A:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_a.png"));
                    break;
                case B:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_b.png"));
                    break;
                case C:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_c.png"));
                    break;
                case D:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case UNDERVOLT:
            switch(bus)
            {
                case A:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_a.png"));
                    break;
                case B:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_b.png"));
                    break;
                case C:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_c.png"));
                    break;
                case D:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case AMPLIMIT:
            switch(bus)
            {
                case A:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_a.png"));
                    break;
                case B:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_b.png"));
                    break;
                case C:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_c.png"));
                    break;
                case D:
                    ui.lblBus2Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
    }
    
    emit sig_busStatus(bus,status);
}


/*!
    \fn FrmPower::setStatusBus1(BUS bus,POWER_STAT status)
 */
void FrmPower::setStatusBus1(BUS bus,POWER_STAT status)
{
    switch(status)
    {
        case POWER_OFF:
            switch(bus)
            {
                case A:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_a.png"));
                    break;
                case B:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_b.png"));
                    break;
                case C:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_c.png"));
                    break;
                case D:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_off_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case POWER_OK:
            switch(bus)
            {
                case A:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_a.png"));
                    break;
                case B:
                    
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_b.png"));
                    break;
                case C:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_c.png"));
                    break;
                case D:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_ok_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case UNDERVOLT:
            switch(bus)
            {
                case A:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_a.png"));
                    break;
                case B:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_b.png"));
                    break;
                case C:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_c.png"));
                    break;
                case D:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_undervolt_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case OVERVOLT:
            switch(bus)
            {
                case A:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_a.png"));
                    break;
                case B:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_b.png"));
                    break;
                case C:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_c.png"));
                    break;
                case D:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_overvolt_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
        case AMPLIMIT:
            switch(bus)
            {
                case A:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_a.png"));
                    break;
                case B:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_b.png"));
                    break;
                case C:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_c.png"));
                    break;
                case D:
                    ui.lblBus1Status->setPixmap(QPixmap(":images/hpsc/lbl_power_amp_limit_d.png"));
                    break;
                case NONE: //keep the compiler happy
                    break;
            }
            break;
    }
    
    emit sig_busStatus(bus,status);
}


/*!
    \fn FrmPower::checkBus(FrmPower::BUS bus)
 */
FrmPower::POWER_STAT FrmPower::checkBus(FrmPower::BUS bus)
{
    POWER_STAT vStat = checkBusV(bus);
    POWER_STAT aStat = checkBusA(bus);
    
    if(vStat == POWER_OK && aStat == POWER_OK) return POWER_OK;
    else if(vStat == OVERVOLT) return vStat;
         else if(aStat == AMPLIMIT) return aStat;
              else return vStat;
    
    
}
